/**
 * LoginProgress.java 2008-05-14
 * 
 * Linker is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker.account;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JWindow;

import linker.gui.LoginWindow;
import linker.list.ViewList;

/**
 * The progress of login. It includes cancel operation and creates the thread to
 * connect to server and login
 * 
 * @version 2008-05-19
 * @author Jianfeng tujf.cn@gmail.com
 * 
 */

public class LoginProgress extends Thread {

	/**
	 * The LocginWindow refer.
	 */
	private LoginWindow loginWindow;

	/**
	 * The username used to login.
	 */
	private String localUsername;

	/**
	 * The password used to login.
	 */
	private String localPassword;

	/**
	 * The host used to connect server.
	 */
	private String localHost;

	/**
	 * The port used to connect server.
	 */
	private String localPort;

	/**
	 * The domain used to connect server.
	 */
	private String localDomain;

	/**
	 * The flag used to record whether it is canceled or not.
	 */
	private boolean canceled = false;

	/**
	 * The area used to show login information.
	 */
	private JTextArea infoArea = new JTextArea();

	/**
	 * The window used to cover loginWindow and show logining area.
	 */
	private final JWindow window;

	/**
	 * The progress of login.
	 * 
	 * @param lg
	 *            The refer of loginWindow
	 * @param username
	 *            The username used to login.
	 * @param password
	 *            The password used to login
	 * @param host
	 *            The server host.
	 * @param port
	 *            The server port.
	 * @param domain
	 *            The server domain
	 */
	public LoginProgress(final LoginWindow lg, final String username,
			final String password, final String host, final String port,
			final String domain) {
		this.loginWindow = lg;
		this.localUsername = username;
		this.localPassword = password;
		this.localHost = host;
		this.localPort = port;
		this.localDomain = domain;

		window = new JWindow();
		window.setSize(loginWindow.getSize());
		window.setLocation(loginWindow.getLocation());
		window.setVisible(true);

		infoArea.setEditable(false);
		infoArea.append("\tConnecting...");
		infoArea.setBackground(Color.LIGHT_GRAY);
		window.add(infoArea, BorderLayout.CENTER);

		JPanel panel = new JPanel();
		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				canceled = true;
				loginWindow.setVisible(true);
			}
		});
		panel.add(cancelButton);
		window.add(panel, BorderLayout.SOUTH);
	}

	/**
	 * The run progress .
	 */
	public final void run() {

		// Creat a thread for login
		Thread t = new Thread() {
			public void run() {
				boolean login = Connect.login(LoginProgress.this,
						localUsername, localPassword, localHost, localPort,
						localDomain);
				if (login) {
					loginWindow.loginSuccess();

					// login successed.
					new Account();
					// view list.
					new ViewList(Connect.getConnecdtion());

					window.setVisible(false);
					canceled = true;
				} else {

					// login failed
					canceled = false;
					loginWindow.setVisible(true);
				}
			}
		};
		t.start();

		// Wait until CancelButton is Clicked or login done;
		final int sleepTime = 100;
		while (!canceled) {
			try {
				sleep(sleepTime);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

		// Stop the login thread.
		Connect.cancel();
		append("\n\tCanceled");
		window.setVisible(false);
	}

	/**
	 * Append info to the display area.
	 * 
	 * @param info
	 *            The info used to append on the display area.
	 */
	public final void append(final String info) {
		infoArea.append(info);
	}
}
